package com.cl.master.controller;

import com.cl.master.common.Result;
import com.cl.master.domain.Company;
import com.cl.master.service.impl.CompanyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author xc
 * @Date 2023/4/26 15:33
 * @Version 1.0
 */
/*遵守Restful风格*/
@Controller
//注解api说明该类需要生成api文档
@Api("企业模块API文档")
public class CompanyController {

    @Autowired
    CompanyServiceImpl companyService;

    //可视化需要数据
    //获得相关所有的数据信息
    //获得相关所有的数据信息
    @RequestMapping("/sewage/getCompanyData")
    @ResponseBody
    // @RequestMapping("getCompanyData")
    public Result getCompanyData(){
        List<Map<String,Object>> data = companyService.getCompanyData();
        Result result = Result.success(data);
        return result;
    }

    //以下都是信息管理数据


    @GetMapping("/company")
    @ApiOperation("显示全部企业")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "model",
                    value = "Model域"
            )
    })
    public String getAllCompany(Model model) {
        Collection<Company> allCompany = companyService.getAllCompany();
        model.addAttribute("allCompany", allCompany);
        System.out.println("所有企业信息查询完毕");
        return "manage/list";
    }

    @PostMapping("/company")
    @ApiOperation("添加企业")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "company",
                    value = "要添加的企业"
            )
    })
    public String addCompany(Company company) {
        companyService.addCompany(company);
        System.out.println("添加企业成功");
        return "redirect:/company";
    }

    @DeleteMapping("/company/{id}")
    @ApiOperation("删除企业")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "=企业ID"
            )
    })
    public String delCompany(@PathVariable int id, HttpServletRequest request, ModelMap model){
        try {
            companyService.delCompany(id);
            companyService.alter();
            System.out.println("删除企业成功");
            return "redirect:/company";
        }  catch (DataIntegrityViolationException e) {
            String message = "无法删除该企业，它仍在使用中！";
            // request.setAttribute("message", message);
            model.put("message", message);
            return "commons/error";
        }catch (Exception e) {
            String message = "无法删除该企业，它仍在使用中22！";
            // request.setAttribute("message", message);
            model.put("message", message);
            return "commons/error";
        }
      }

    @GetMapping("/toUpdateCompany/{id}")
    @ApiOperation("去编辑企业页面")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "企业ID"
            ),
            @ApiImplicitParam(
                    name = "model",
                    value = "Model域"
            )
    })
    public String toUpdateCompany(@PathVariable int id,Model model){
        Company company = companyService.getCompanyById(id);
        model.addAttribute("company",company);
        System.out.println(company);
        return "company/updateCompany";
    }

    @PutMapping("/company")
    @ApiOperation("编辑企业")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "company",
                    value = "要编辑的企业"
            )
    })
    public String updateCompany(Company company,ModelMap model){
        try {
            companyService.updateCompany(company);
            System.out.println(company);
            return "redirect:/company";
        }catch (DataIntegrityViolationException e) {
            String message = "无法编辑该企业编码，以外键形式与排污数据信息表相连!";
            // request.setAttribute("message", message);
            model.put("message", message);
            return "commons/error";
        }catch (Exception e) {
            String message = "无法编辑该企业编码，以外键形式与排污数据信息表相连!22";
            // request.setAttribute("message", message);
            model.put("message", message);
            return "commons/error";
        }
    }
}
